<?php
/**
 * Copyright (c) 2010, Slawomir Mazur <mazursllawek@gmail.com>
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions 
 * are met:
 * 
 * o Redistributions of source code must retain the above copyright notice, 
 *   this list of conditions and the following disclaimer.
 * o Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * o Neither the name of the Softisland nor the names of its contributors
 *   may be used to endorse or promote products derived from this software
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * Enter description here...
 *
 * @package     Admin
 * @subpackage  Admin_Dashboard
 * @copyright   Slawomir Mazur (c) 2010 (http://www.zend.com/zce.php?c=ZEND011643)
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @author      Slawomir Mazur <mazursllawek@gmail.com>
 */

class Admin_DashboardController extends Zend_Controller_Action
{
	const FORM_GROUP_NAME = 'dashboard';
	
	public function indexAction() { }
	
	/**
	 * 
	 * Upload action
	 */
    public function uploadAction()
    {
    	$headScripts = $this->view->headScript();

    	$headScripts->appendFile($this->view->baseUrl('js/upload/jq.fileupload-ui.js'));
		$headScripts->appendFile($this->view->baseUrl('js/upload/jq.fileupload.js'));
		$headScripts->appendFile($this->view->baseUrl('js/jq.sprintf.js'));
    }
    
    /**
     * 
     * Upload files action. Request is AJAX
     */
    public function uploadPostAction()
    {
    	$this->_helper->viewRenderer->setNoRender();
    	$response = array('file' => $this->view->translate('no file available'));
    	
    	if (!$this->getRequest()->isPost()) {
    		$this->_redirect('admin/dashboard/upload');
    	}
    	
    	// start upload
    	try {
    		$uploader = new Gp_File_Uploader('library');
    		// uploaded file name (Exception occures)
    		$response['file'] = $uploader->getFileName();
    		
    		$uploader->setAllowedExtensions(array('csv'));
			$uploader->setAllowRenameFiles(true);
			$uploader->save(Gp_File_Excel::BASIC_EXCEL_UPLOAD_PATH);
    		
			// real file name after upload
			$response['file'] = $uploader->getUploadedFileName();
    		
			// create database entry for file
			$library = Gp_App::getModel('system/library')->createRow();
			$library->name = $uploader->getUploadedFileName();
			$library->created_at = now();
			$library->status = System_Model_Library::UPLOADED;
			$library->save();
			
    		$response = array(
    			'libraryId' => $library->library_id,
    			'message' => $this->view->translate('File [%s] uploaded succesfully', $response['file']),
    			'status' => Gp_Controller_Action_Helper_Messenger::SUCCESS
    		);
    	} catch (Exception $e) {
    		$response = array(
    			'message' => $this->view->translate('[%s] %s', $response['file'], $e->getMessage()),
    			'status' => Gp_Controller_Action_Helper_Messenger::ERROR
    		);
    		Gp_App::logException($e);
    	}
    	
    	if ($this->getRequest()->isXmlHttpRequest()) {
    		$this->_helper->layout()->disableLayout();
    		$this->getResponse()->clearBody();
    		
			$this->getResponse()->setBody(Zend_Json::encode($response));
    		return;
    	} else {
			$this->_helper->getHelper(Gp_View_Helper_Messenger::ADMIN_MESSENGER)
						->addMessage($response['message'], $response['status']);
    		$this->_redirect('admin/dashboard/upload');
    	}
    }
    
	public function settingsAction()
    {
    	$this->view->settingsForm = $this->_getSettingsForm();
    }
    
    public function settingsPostAction()
    {
		$this->_helper->viewRenderer->setNoRender();
		
		
		
		$form = $this->_getSettingsForm();
		
		
    }
    
	protected function _getSettingsForm()
    {
    	$form = new Gp_Form();
    	$form->setAction($this->view->baseUrl('admin/dashboard/settings-post'))
			 ->setMethod('post')
			 ->setAttrib('id', 'settings')
			 ->setButtonsToReset(array('submit'))
			 ->setElementsBelongTo(self::FORM_GROUP_NAME);

		$submit = new Zend_Form_Element_Button('submit');
		$submit->setAttrib('type', 'submit');

        $form->addElements(array($submit));
			 
		return $form;
    }
}